﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

//namespace EmptyCollectionDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            int[] numbers1 = new int[] { 1, 2, 3, };
//            int[] numbers2 = new int[] { 4, 5, 6 };
//            int[] numbers3 = new int[] { 7, 8, 9 };

//            List<int[]> all = new List<int[]> { 
//                numbers1, numbers2, numbers3 };

//            //var allNumbers = all.Aggregate(
//            //    Enumerable.Empty<int>(), (current, next) =>
//            //    current.Union(next));

//            //var allNumbers = all.Aggregate((current, next) =>
//            //    current.Union(next).ToArray<int>());

//            var allNumbers = Enumerable.Aggregate(all, Enumerable.Empty<int>(),
//                (current, next) => current.Union(next));

//            Array.ForEach(allNumbers.ToArray(), n => Console.WriteLine(n));
//            Console.ReadLine();
//        }
//    }
//}

//namespace AverageDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            int[] numbers = new int[] { 13, 24, 5, 6, 78, 23 };
//            Console.WriteLine(numbers.Average());
//            Console.ReadLine();
//        }
//    }
//}

//namespace AverageFileSizeDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            string[] files = Directory.GetFiles("C:\\temp\\");

//            double averageFileSize = (from file in files select (new FileInfo(file)).Length).Average();
//            averageFileSize = Math.Round(averageFileSize / 1, 2);
//            Console.WriteLine("Average file size (in c:\\temp) {0} bytes", averageFileSize);
//            Console.ReadLine();
//        }
//    }
//}

//namespace AverageWordLengthInFile
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            string[] lines = File.ReadAllLines("..\\..\\WalrusAndCarpenter.txt");
//            // sanity check
//            Array.ForEach(lines, line => Console.WriteLine(line));

//            Func<string, string[]> Split = str => str.Split(new char[] { '-', ',', ' ', '.' }, 
//                StringSplitOptions.RemoveEmptyEntries);

//            List<string> all = new List<string>();
//            // split out word
//            Array.ForEach(lines, line => all.AddRange(Split(line)));

//            // sanity check
//            Array.ForEach(all.ToArray(), word => Console.WriteLine(word));

//            // send result
//            Console.WriteLine("Average word length {0}",
//                Math.Round(all.Average(w => w.Length), 2));

//            Console.ReadLine();
//        }
//    }
//}

//namespace MinMaxNumber
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            var numbers = new float[] { 1.0f, 3.5f, -7.8f, 10f };
//            Console.WriteLine("Min: {0}", numbers.Min());
//            Console.WriteLine("Max: {0}", numbers.Max());
//            Console.ReadLine();
//        }
//    }
//}

//namespace MinWordDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            var games = new string[] { "Halo 3", "Call of Duty 4", "Project Gotham 3" };
//            Console.WriteLine("Shortest title: {0}", games.Min(t => t.Length));

//            // short title name
//            var title = from game in games
//                        where game.Length == games.Min(t => t.Length)
//                        select game;

//            Array.ForEach(title.ToArray(), s => Console.WriteLine(s));
//            Console.ReadLine();
//        }
//    }
//}

//namespace MinMaxDemo
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            string[] files = Directory.GetFiles("C:\\WINDOWS\\Temp", 
//                "*.*", SearchOption.AllDirectories);
//            Console.WriteLine(files.Count());

//            Action<long> Print = l =>
//            {
//                Debug.Print(l.ToString());
//            };

//            // gets maximun
//            var max = (from fileName in files
//                       let info = new FileInfo(fileName)
//                       orderby info.Length descending
//                       select
//                       new { FileName = info.Name, Size = info.Length }).Take(1);
//            Stopwatch sp = new Stopwatch();
//            sp.Start();
//            Console.WriteLine("Using Take: {0}", max.ElementAt(0));
//            sp.Stop();
//            Print(sp.ElapsedMilliseconds);

//            // with anonymous type we have to indicate what to get the max of 
//            sp.Reset();
//            sp.Start();
//            var max1 = (from fileName in files
//                        let info = new FileInfo(fileName)
//                        select
//                        new { FileName = info.Name, Size = info.Length }).Max(s => s.Size);
//            Console.WriteLine("Using Max with anonymous type: {0}", max1);
//            sp.Stop();
//            Print(sp.ElapsedMilliseconds);

//            // with named type we lose convenience of anonoymous type but get
//            // inheritance, realization, for example we can implement IComparable
//            // and get the max of the whole object
//            sp.Reset();
//            sp.Start();
//            var max2 = (from fileName in files
//                        let info = new FileInfo(fileName)
//                        select
//                        new Temp { FileName = info.Name, Size = info.Length }).Max();

//            Console.WriteLine("Using Max: {0}", max2);
//            sp.Stop();
//            Print(sp.ElapsedMilliseconds);

//            Console.ReadLine();
//        }

//        public class Temp : IComparable<Temp>
//        {
//            public string FileName { get; set; }
//            public long Size { get; set; }

//            public int CompareTo(Temp other)
//            {
//                return Size.CompareTo(other.Size);
//            }

//            public override string ToString()
//            {
//                return string.Format("FileName: {0}, Size: {1}", FileName, Size);
//            }
//        }
//    }
//}

//namespace SummingColumnValues
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            string[] lines = File.ReadAllLines("..\\..\\csvData.txt");

//            // make sure we have some data
//            foreach (string s in lines)
//                Console.WriteLine("{0}", s);

//            var results =
//                from line in lines
//                let values = line.Split(',')
//                let y = values
//                select (from str in y select Convert.ToInt32(str));

//            Console.WriteLine();
//            for (int i = 0; i < results.Count(); i++)
//            {
//                Console.WriteLine("Total Column {0}: {1}", i + 1,
//                    results.Sum(o => o.ElementAt(i)));
//            }
//            System.Threading.Thread.Sleep(5000);
//        }
//    }
//}

namespace MedianOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            var numbers = new int[] { 3, 4, 5, 6, 7, 8, 9, 10 };
            Console.WriteLine(numbers.Median());
            Console.ReadLine();
        }
    }

    public static class MyAggragate
    {
        public static float Median(this IEnumerable<int> source)
        {
            int middleIndex = source.Count() / 2;
            if (source.Count() % 2 != 0)
                return source.ToArray()[middleIndex];
            else
                return (source.ElementAt(middleIndex - 1) + source.ElementAt(middleIndex)) / 2f;
            
        }
    }
}